package leetcode.code0950;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;

//执行用时：3 ms, 在所有 Java 提交中击败了90.78%的用户
//
//内存消耗：41.2 MB, 在所有 Java 提交中击败了24.76%的用户
//
//通过测试用例：33 / 33
public class Solution implements IDebug {

	public int[] deckRevealedIncreasing(int[] deck) {
		int len = deck.length;
		Arrays.sort(deck);
		int[] ans = new int[len];
		Queue<Integer> queue = new LinkedList<>();
		boolean flag = true;
		int pdeck = 0;
		for (int i = 0; i < len; i++) {
			if (flag) {
				ans[i] = deck[pdeck++];
			} else {
				queue.add(i);
			}
			flag = !flag;
		}
		while (!queue.isEmpty()) {
			int i = queue.poll();
			if (flag) {
				ans[i] = deck[pdeck++];
			} else {
				queue.add(i);
			}
			flag = !flag;
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
